using System.Collections.Generic;
using UnityEngine;

public class FriendProfileDisplayController : MonoBehaviour
{
	public struct FriendsAtStageCount
	{
		public int currentButtonOffset;

		public int totalFriendsAtStage;
	}

	public UIPanel scrollView;

	public GameObject friendButtonPrefab;

	public Vector3 buttonStageOffset;

	public Vector3 buttonStageTournamentOffset;

	public Vector3 multipleFriendsOffset;

	public Vector3 expansionHorizontalOffset;

	public Vector3 expansionVerticalOffset;

	public FriendProfileButton myProfileButton;

	public Vector3 myProfileOffset;

	public Vector3 myProfileTournamentOffset;

	private bool hasFetchedMyPicture;

	public int maxFriendsAtSinglePosition = 3;

	private List<FriendProfileButton> profileButtons = new List<FriendProfileButton>();

	private List<FriendsAtStageCount> friendProgressCount = new List<FriendsAtStageCount>();

	private int fetchFriendPicturesRequestTicket = -1;

	private int outstandingTextureRequests;

	public PlayerCareerStages stageDefinition
	{
		get
		{
			return ScriptableObjectSingleton<PlayerCareerStages>.instance;
		}
	}

	private void Awake()
	{
		BehaviourSingletonInit<FriendProfilesManager>.instance.onProfilesAvailable -= UpdateProfileButtons;
		BehaviourSingletonInit<FriendProfilesManager>.instance.onProfilesAvailable += UpdateProfileButtons;
		hasFetchedMyPicture = false;
		UpdateProfileButtons();
	}

	private void OnDestroy()
	{
		if (fetchFriendPicturesRequestTicket >= 0 && BehaviourSingleton<GGRequestLimitter>.instance != null)
		{
			BehaviourSingleton<GGRequestLimitter>.instance.StopRequestsWithGroup(fetchFriendPicturesRequestTicket);
		}
		if (BehaviourSingletonInit<FriendProfilesManager>.instance != null)
		{
			BehaviourSingletonInit<FriendProfilesManager>.instance.onProfilesAvailable -= UpdateProfileButtons;
		}
	}

	public void UpdateProfileButtons()
	{
		fetchFriendPicturesRequestTicket = BehaviourSingleton<GGRequestLimitter>.instance.GetGroupId();
		FriendProfileConfig.FriendProfileWrapper myProfile = BehaviourSingletonInit<FriendProfilesManager>.instance.myProfile;
		if (myProfile != null)
		{
			int groupId = -9999;
			if (myProfile.pictureUrl != string.Empty && !hasFetchedMyPicture)
			{
				groupId = fetchFriendPicturesRequestTicket;
				outstandingTextureRequests++;
				hasFetchedMyPicture = true;
			}
			InitButton(myProfileButton, myProfile, myProfileButton.friendProfile == null, groupId, true);
		}
		List<FriendProfileConfig.FriendProfileWrapper> friendsProfiles = BehaviourSingletonInit<FriendProfilesManager>.instance.friendsProfiles;
		AddFriendProgressCounts(friendProgressCount, friendsProfiles);
		foreach (FriendProfileButton profileButton in profileButtons)
		{
			FriendProfileConfig.FriendProfileWrapper friendProfileWrapper = TryGetProfile(friendsProfiles, profileButton.friendId);
			InitButton(profileButton, friendProfileWrapper, false, -9999, false);
			int index = ((friendProfileWrapper != null) ? Mathf.Min(friendProfileWrapper.maxStageReached, stageDefinition.stages.Count - 1) : Mathf.Min(profileButton.maxStageIndex, stageDefinition.stages.Count - 1));
			friendProgressCount[index] = new FriendsAtStageCount
			{
				currentButtonOffset = friendProgressCount[index].currentButtonOffset - 1,
				totalFriendsAtStage = friendProgressCount[index].totalFriendsAtStage
			};
		}
		foreach (FriendProfileConfig.FriendProfileWrapper item in friendsProfiles)
		{
			FriendProfileButton friendProfileButton = TryFindFriendInButtons(item.fbIdForApp);
			if (!(friendProfileButton != null))
			{
				int index2 = Mathf.Min(item.maxStageReached, stageDefinition.stages.Count - 1);
				int num = friendProgressCount[index2].totalFriendsAtStage - friendProgressCount[index2].currentButtonOffset;
				if (num < maxFriendsAtSinglePosition)
				{
					friendProfileButton = NGUITools.AddChild(friendButtonPrefab.transform.parent.gameObject, friendButtonPrefab).GetComponent<FriendProfileButton>();
					profileButtons.Add(friendProfileButton);
					InitButton(friendProfileButton, item, true, fetchFriendPicturesRequestTicket, false);
					outstandingTextureRequests++;
					friendProgressCount[index2] = new FriendsAtStageCount
					{
						currentButtonOffset = friendProgressCount[index2].currentButtonOffset - 1,
						totalFriendsAtStage = friendProgressCount[index2].totalFriendsAtStage
					};
				}
			}
		}
	}

	private void InitButton(FriendProfileButton button, FriendProfileConfig.FriendProfileWrapper profile, bool isFirstInit, int groupId, bool isMe)
	{
		int num = ((profile != null) ? Mathf.Min(profile.maxStageReached, this.stageDefinition.stages.Count - 1) : Mathf.Min(button.maxStageIndex, this.stageDefinition.stages.Count - 1));
		PlayerCareerStageDefinition.StageDefinition stageDefinition = this.stageDefinition.stages[num];
		FriendsAtStageCount friendsAtStageCount2;
		if (isMe)
		{
			FriendsAtStageCount friendsAtStageCount = default(FriendsAtStageCount);
			friendsAtStageCount.currentButtonOffset = 1;
			friendsAtStageCount.totalFriendsAtStage = 1;
			friendsAtStageCount2 = friendsAtStageCount;
		}
		else
		{
			friendsAtStageCount2 = friendProgressCount[num];
		}
		FriendsAtStageCount stageCount = friendsAtStageCount2;
		int num2 = stageCount.totalFriendsAtStage - stageCount.currentButtonOffset;
		bool flag = stageDefinition.subStages.Count > 0;
		Vector3 vector = ((!isMe) ? (buttonStageOffset + num2 * multipleFriendsOffset) : myProfileOffset);
		if (flag)
		{
			vector = ((!isMe) ? (buttonStageTournamentOffset + num2 * multipleFriendsOffset) : myProfileTournamentOffset);
		}
		if (isFirstInit)
		{
			button.transform.localPosition = stageDefinition.position + vector;
		}
		int num3 = ((!isFirstInit) ? Mathf.Min(button.maxStageIndex, this.stageDefinition.stages.Count - 2) : num);
		List<Vector3> list = new List<Vector3>();
		for (int num4 = num; num4 >= num3; num4--)
		{
			list.Add(this.stageDefinition.stages[num4].position + vector);
		}
		button.Init((profile == null) ? button.friendProfile : profile, list, stageCount, groupId, this);
	}

	private FriendProfileConfig.FriendProfileWrapper TryGetProfile(List<FriendProfileConfig.FriendProfileWrapper> profiles, string friendId)
	{
		foreach (FriendProfileConfig.FriendProfileWrapper profile in profiles)
		{
			if (profile.fbIdForApp == friendId)
			{
				return profile;
			}
		}
		return null;
	}

	private void AddFriendProgressCounts(List<FriendsAtStageCount> friendProgressCount, List<FriendProfileConfig.FriendProfileWrapper> profiles)
	{
		for (int i = 0; i < stageDefinition.stages.Count; i++)
		{
			if (friendProgressCount.Count <= i)
			{
				friendProgressCount.Add(new FriendsAtStageCount
				{
					totalFriendsAtStage = 0,
					currentButtonOffset = 0
				});
			}
			else
			{
				friendProgressCount[i] = new FriendsAtStageCount
				{
					currentButtonOffset = friendProgressCount[i].totalFriendsAtStage,
					totalFriendsAtStage = friendProgressCount[i].totalFriendsAtStage
				};
			}
		}
		foreach (FriendProfileConfig.FriendProfileWrapper profile in profiles)
		{
			FriendProfileButton friendProfileButton = TryFindFriendInButtons(profile.fbIdForApp);
			if (friendProfileButton != null)
			{
				int index = Mathf.Min(friendProfileButton.maxStageIndex, stageDefinition.stages.Count - 2);
				friendProgressCount[index] = new FriendsAtStageCount
				{
					currentButtonOffset = friendProgressCount[index].totalFriendsAtStage - 1,
					totalFriendsAtStage = friendProgressCount[index].totalFriendsAtStage - 1
				};
			}
			int index2 = Mathf.Min(profile.maxStageReached, stageDefinition.stages.Count - 1);
			friendProgressCount[index2] = new FriendsAtStageCount
			{
				currentButtonOffset = friendProgressCount[index2].totalFriendsAtStage + 1,
				totalFriendsAtStage = friendProgressCount[index2].totalFriendsAtStage + 1
			};
		}
	}

	private FriendProfileButton TryFindFriendInButtons(string friendId)
	{
		foreach (FriendProfileButton profileButton in profileButtons)
		{
			if (profileButton.friendId == friendId)
			{
				return profileButton;
			}
		}
		return null;
	}

	public void OnTextureArrived()
	{
		outstandingTextureRequests--;
		if (outstandingTextureRequests <= 0)
		{
			outstandingTextureRequests = 0;
			fetchFriendPicturesRequestTicket = -1;
		}
	}

	public void ExpandButtonsAtStage(int stage)
	{
		int num = Mathf.Min(stage, stageDefinition.stages.Count - 1);
		Vector3 vector = stageDefinition.stages[num].position + buttonStageOffset;
		int num2 = 0;
		foreach (FriendProfileButton profileButton in profileButtons)
		{
			int num3 = Mathf.Min(profileButton.maxStageIndex, stageDefinition.stages.Count - 1);
			if (num3 == num)
			{
				num2++;
				if (num2 % 2 == 0)
				{
					int num4 = num2 / 2;
					profileButton.DoExpandView(vector + num4 * expansionHorizontalOffset);
				}
				else if (num2 > 1)
				{
					int num5 = num2 / 2;
					profileButton.DoExpandView(vector + num5 * expansionVerticalOffset);
				}
			}
		}
	}
}
